package com.hnkywl.sts.dao.jkgl;

import com.hnkywl.sts.entity.jkgl.XsSettle;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.SQLQuery;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 销售系统结算信息相关Dao
 * Created by Fei on 2015/12/22.
 */
@Repository
public class XsSettleDao extends SimpleHibernateTemplate<XsSettle, Long> {

    /**
     * 统计销售系统成交量
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public List<Map> countSettleWtByDate(String dateStart, String dateEnd) {
        String query = "SELECT SUM(SETTLE_WT) as SETTLE_WT,SUM(SETTLE_AMT) AS SETTLE_AMT FROM T_XS_SETTLE_INFO WHERE 1=1";
        String whereSql = "";
        Map<String, Object> param = new HashMap<String, Object>();

        if(!"".equals(dateStart)){
            whereSql += " AND SETTLE_DATE >= :dateStart";
            param.put("dateStart",dateStart.replace("-", ""));
        }

        if(!"".equals(dateEnd)){
            whereSql += " AND SETTLE_DATE < :dateEnd";
            param.put("dateEnd",dateEnd.replace("-", ""));
        }

        SQLQuery sqlQuery = getSession().createSQLQuery(query + whereSql);
        sqlQuery.setProperties(param);
        sqlQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        return sqlQuery.list();
    }

//    /** 查询最新的一页数据给交易大屏 */
//    public List<Map<String, Object>> getXSSettleDetail(String dateStart, String dateEnd) {
//        String query = "select substr(create_date,9,2)||':'||substr(create_date,11,2) \"settleTime\"," +
//                "COMPANY_NAME \"companyName\",ORDER_ITEM_NUM \"orderItemNum\",SETTLE_WT \"settleWt\",SETTLE_AMT \"settleAmt\" " +
//                "from (select * from T_XS_SETTLE_INFO  WHERE 1=1";
//        String whereSql = "";
//        Map<String, Object> param = new HashMap<String, Object>();
//
//        if(!"".equals(dateStart)){
//            whereSql += " AND SETTLE_DATE >= :dateStart";
//            param.put("dateStart",dateStart.replace("-", ""));
//        }
//
//        if(!"".equals(dateEnd)){
//            whereSql += " AND SETTLE_DATE < :dateEnd";
//            param.put("dateEnd",dateEnd.replace("-", ""));
//        }
//        whereSql += " and settle_wt > 0 order by create_date desc,id desc) where rownum <= 4";
//        SQLQuery sqlQuery = getSession().createSQLQuery(query + whereSql);
//        sqlQuery.setProperties(param);
//        sqlQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
//        return sqlQuery.list();
//    }
}
